(function(UI,Widget){
	var _com = Widget.Commponents = $.IAdapter({
		mapping:{
			build:function(ctr,op){
				if(this.check(op))
					this.initCommponent(ctr,op,op.handler);
			},
			check:function(op){
				return false;
			},
			initCommponent:function(ctr,op){}
		}
	});
	//toolbar组件
	_com.add("toolBar",{
		check:function(op){
			return op.buttons && op.buttons.length;
		},
		initCommponent:function(ctr,op,handler){
			handler.complete.add(function(ctr,op){
				var toolBar = $.create("div").appendTo(ctr.getBody());
				toolBar.ccayToolbar({
					items : op.buttons
				});
				delete op.buttons;
			});
		}
	});
	
	//事件组件
	_com.add("events",{
		check:function(op){
			return op.events;
		},
		initCommponent:function(ctr,op,handler){
			handler.complete.add(function(ctr,op){
				var panel = ctr.dom;
				$.each(op.events,function(ev,fn){
					var tm = ev.split(" "),
						target = tm[0],
						evName = tm[1];
						
					panel.find(target).bind(evName,ctr,function(e){
						fn.call(this,e,e.data);
					});
				})
				delete op.events;
			});
		}
	});

	//个性化组件
	UI.XControl.extendIManager("widget",{
		getCustomSetting:function(){
			return this.op._customSettings;
		}
	});
	
	_com.add("customSetting",{
		check:function(op){
			return op.customSetting !== false;
		},
		initCommponent:function(ctr,op,handler){
			op.noSetFields || (op.noSetFields = []);
			op.layoutItemCss || (op.layoutItemCss = 'layout-item');
			op.customSettingTarget || (op.customSettingTarget = '.layout-title') ;
			if(op.convertMode)
				ctr.dom.attr('config','{ignore:null}');
			else
				this.setHideField(ctr,op,handler);
				
			handler.complete.add(this.initSetting);
		},
		setHideField:function(ctr,op,handler){
			handler.initComplete.add(function(ctr,op){
				if(op.view.validOp)
					op.view.validOp.ignore = null;
				else
					op.view.validOp = {ignore:null};
			})
			
			handler.buildField.add(function(field,op){
				if(field.customSetting === false || Ccay.Modal.isRequiredField(field))
				{
					op.noSetFields.push(field.field);
					if(field.layout)
						field.layout += " widget-item-noset";
					else
						field.layout = " widget-item-noset";
				}
			});
		},
		initSetting:function(ctr,op){
			if(op.convertMode)
			{
				var item,input,target;
				ctr.getBody().find('.' + op.layoutItemCss).each(function(){
					item = $(this);
					input = item.find('[name]:first');
					target = item.find(op.customSettingTarget).addClass('layout-title');
					input.length && item.attr('field',input.attr("name"));

					if(target.hasClass('required'))
						item.addClass("widget-item-noset");
				});
			}
			
			ctr.getHeadBar().append(
				Ccay.UI.newButton({type:"img",css:"setting",attr:{eno:'customSetting'},text:"ccay.common.widget.customSetting"}),
				Ccay.UI.newButton({css:"hide",attr:{eno:'save'},text:"ccay.common.label.save"}),
				Ccay.UI.newButton({css:"hide",attr:{eno:'cancel'},text:"ccay.common.button.cancel"})
			);
			$.extend(op.headOpeartion,settingOperation);
			// 获取保存的个性化设置
			var settings = Ccay.Personalized.getCurrPageSettings(ctr.id), settingCtr;
			if(settings)
			{
				settingCtr = op._customSettings = settings;
				if(settingCtr.hideFields)
				{
					$.each(settingCtr.hideFields,function(name){
						ctr.find('.' + op.layoutItemCss + '[field="' + name + '"]').addClass("widget-item-hide ccay-hideValid");
					});
				}
				else
					settingCtr.hideFields = {};
			}
			else
				op._customSettings = {hideFields:{}};
				
			
		}	
	});
	var settingOperation = {
		customSetting:function(e,ctr){
			var toolBar = this.parent();
			this.hide().next().show().next().show();
			ctr.dom.addClass('widget-customSetting');
			//ctr.reBuild();
			var body = ctr.getBody();
			if(!body.data("buildMenu"))
			{
				var triggerCss = ctr.op.layoutItemCss,
					menuTarget = ctr.op.customSettingTarget;
				body.data("buildMenu",true).contextMenu({
					target: triggerCss,
					menus:settingOperation.menus,
					fnGetTarget:function(item,cMenu,fixed,op){
						var target = item.find(menuTarget);
						if(target.length){
							fixed.left = target.outerWidth() - cMenu.outerWidth();
						}else{
							target = item;
						};
						return target;
					},
					fnHover:function(target){
						//判断对象是否为不可设置项
						return !target.hasClass("widget-item-noset");
					}
				});
			}
			else
				body.enableContextMenu(true);
			ctr.getBody().find('.widget-item-hide:visible').css("display","");
			// 个性化说明提示
//			ctr.getHeadBar().tip({content:"ccay.common.widget.operateTip", addTipStyle: false, show: true, autoClose: false});
		},
		save:function(e,ctr){
			this.hide().next().hide();
			this.prev().show();
			var item,field,hideFields = ctr.op._customSettings.hideFields, customSetting = {};
			ctr.getBody().find('.widget-item-temphide,.widget-item-tempshow').each(function(i){
				item = $(this),field = item.attr("field");
				if(item.hasClass("widget-item-temphide"))
				{
					item.addClass("widget-item-hide ccay-hideValid").removeClass("widget-item-temphide");
					hideFields[field] = null;
				}
				else
				{
					item.removeClass("widget-item-hide").removeClass("widget-item-tempshow");
					delete hideFields[field];
				}
			});
			settingOperation.end(ctr);
			// 保存个性化设置
			Ccay.Personalized.saveSetting(ctr.id,ctr.getCustomSetting());
		},
		cancel:function(e,ctr){
			this.hide().prev().hide().prev().show();
			ctr.getBody().find('.widget-item-temphide,.widget-item-tempshow').removeClass("widget-item-temphide").removeClass("widget-item-tempshow");
			settingOperation.end(ctr);
		},
		end:function(ctr){
			ctr.getBody().enableContextMenu();
			ctr.dom.removeClass('widget-customSetting');
			// 清除Tip
//			ctr.getHead().children(".ccay-tip").remove();
		},
		menus:[
			{text:"ccay.common.widget.hideItem",css:"hidden",click:function(target){
				target = target.parent();
				if(target.hasClass('widget-item-tempshow'))
					target.removeClass('widget-item-tempshow');
				else
					target.addClass('widget-item-temphide');
					
			},fnCheck:function(target,menu,data){
				target = target.parent();
				return target.hasClass('widget-item-tempshow') || (!target.hasClass('widget-item-hide') && !target.hasClass('widget-item-temphide'));
			}},
			{text:"ccay.common.widget.showItem",css:"show",click:function(target){
				target = target.parent();
				if(target.hasClass('widget-item-temphide'))
					target.removeClass('widget-item-temphide');
				else
					target.addClass('widget-item-tempshow');
			},fnCheck:function(target,menu,data){
				target = target.parent();
				return !target.hasClass('widget-item-tempshow') && (target.hasClass('widget-item-hide') || target.hasClass('widget-item-temphide'));
			}}
		]
	};
	
	//折叠
	_com.add("shrink",{
		check:function(op){
			return op.miniable !== false;
		},
		initCommponent:function(ctr,op,handler){
			handler.complete.add(function(ctr,op){
				// 同步个性化设置是否是最小化状态
				var customSetting = ctr.getCustomSetting(),
					minCss = "panel-minimize",
					norCss = "panel-normal hide";
				if (customSetting && customSetting.isShrink) {
					minCss = "panel-minimize hide";
					norCss = "panel-normal";
					ctr.getHead().addClass('widget-mininize');
					op._hideBody = true;
				}
				ctr.getHeadBar().append(
				Ccay.UI.newButton({type:"img",css: minCss, attr:{eno:'minimize'},text:"ccay.common.widget.minimize"}),
				Ccay.UI.newButton({type:"img",css: norCss, attr:{eno:'normal'},text:"ccay.common.widget.normal"})
				);
			});
			$.extend(op.headOpeartion,shrinkOperation);
		}
	});
	var shrinkOperation = {
		minimize:function(e,ctr){
			this.hide().next().show();
			ctr.getHead().addClass('widget-mininize').next().slideUp().next().hide();
			shrinkOperation.saveShrink(ctr, true);
		},
		normal:function(e,ctr){
			this.hide().prev().show();
			ctr.getHead().removeClass('widget-mininize').next().slideDown().next().show();
			shrinkOperation.saveShrink(ctr, false);
		},
		saveShrink: function (ctr, isShrink){
			// 获取个性化设置
			if (ctr.id) {
				Ccay.Personalized.saveSetting(ctr.id,{"isShrink": isShrink});
			}
		}
	};
})(Ccay.UI,Ccay.UI.Widget);